/*******************************************************************************  
* 文件名称：蓝桥杯单片机第十二届省赛
* 实验目的：完成赛题要求
* 程序说明：
* 字体格式：UTF-8
* 日期版本：2023-3-26	V1.0
			16:00~
*******************************************************************************/

/*********头文件*********/
#include <STC15F2K60S2.H>
#include "intrins.h"
#include "iic.h"
#include "SMG.h"
#include "Timer.h"
#include "Key.h"

/*********宏定义*********/
#define uchar unsigned char 
#define uint unsigned int

uchar PCF8591_ADC_TimeFlag = 0;	//900ms读取ADC输出时间标志位
uchar Frequency_ReadData_Flag = 0;
unsigned char uc_LED;

void LED_Disp(unsigned char uc_LED);

void LED_Function(void)
{
	
	P0 = 0xFF;	
	P2 = P2 & 0X1f |0X80; 
	

	if (menu_index == 0){P0 = 0xFF; P0 &= 0xFB;}							//频率界面, L3
	else if (menu_index == 1){P0 = 0Xff; P0 &= 0xF7;}						//周期界面, L4
	else if (menu_index == 2 | menu_index == 3){P0 = 0Xff; P0 &= 0xEF;}		//电压界面, L5
	
	if (f_PCF8591__ADC_V_Data > Temp_Vol_compare)	{P0 &= 0XFE;}	//通道3实时电压数据比缓存电压数据大，L1点亮
	if (Frequency_Data > Temp_Frequency_compare) 	{P0 &= 0XFD;}	//实时频率大于缓存频率数据，L2点亮	
	
	P2 &= 0x1F;	//关闭573
}


void main()
{
	Power_All_Init();
	
	Timer0_Init();
	Timer2_Init();
	
	while(1)
	{
		SMG_Show_Data();	//数码管显示
		Key4_Proc();		//按键扫描
		LED_Function();
		
		if (Frequency_ReadData_Flag == 1)	//1s读取频率标志位 时间到
		{
			//读取频率
			Frequency_ReadData_Flag = 0;
			TR0 = 0;
			Frequency_Data = TH0*256+TL0;
			Frequency_Data = Frequency_Data * 2;
			Period_Data = 1000000 / Frequency_Data;
			TL0=0;
			TH0=0;
			TR0=1;
		}
		
		if (PCF8591_ADC_TimeFlag == 1)
		{
			PCF8591_ADC_TimeFlag = 0;
			
			PCF8591_Vol_Data  = PCF8591_ADC_Read(0x01);	//0x03电压数据 范围0~255
			f_PCF8591__ADC_V_Data = (float)PCF8591_Vol_Data * 5 / 255;	//0~5V的数据范围
			
			PCF8591_R_Data  = PCF8591_ADC_Read(0x03);	//0x01光敏数据 范围0~255	
		}
		
		
	}
}



////定时器0中断服务函数
//void Timer0_Rountine(void) interrupt 1
//{
//	static unsigned int  T0Count0;
//	

//	T0Count0++;
//	if(T0Count0 >= 1000)
//	{
//		//执行操作
//		
//		T0Count0 = 0;	//软件复位
//	}
//	
//	
//}	


// 定时器2 中断服务函数模板
void Timer2_Routine() interrupt 12  // 1T 16位自动重载
{
    static unsigned int  T2Count0;
	static unsigned char i, smg_scan_cnt;	//数码管
	static unsigned char key4_count;	//独立按键
	static unsigned int  PCF8591_ADC_cnt;
	static unsigned int  Frequency_cnt;
	T2Count0++;	
	if(T2Count0 >= 1000)
	{
		//执行操作
		
		T2Count0 = 0;
	}
	
	smg_scan_cnt++;				//数码管扫描时间
	if (smg_scan_cnt >= 1)		//2ms扫描一次数码管
	{
		smg_scan_cnt = 0;		//软件复位
		P2 = 0xE0; P0 = 0xFF;	//定时器扫描数码管，先消影，就不会出现残影了!!！
		
		P2 = 0xC0; P0 = 0x01<<i;
		
		if (menu_index == 0) {P2 = 0xE0; P0 = menu1[i]; P2 = 0;}
		if (menu_index == 1) {P2 = 0xE0; P0 = menu2[i]; P2 = 0;}
		if (menu_index == 2) {P2 = 0xE0; P0 = menu3[i]; P2 = 0;}
		if (menu_index == 3) {P2 = 0xE0; P0 = menu4[i]; P2 = 0;}
		if (menu_index == 4) {P2 = 0xE0; P0 = menu5[i]; P2 = 0;}
		if (menu_index == 5) {P2 = 0xE0; P0 = menu6[i]; P2 = 0;}
		i ++;
		i = i % 8;
	}
	
	if (++key4_count >= 10)
	{
		key4_count = 0;
		uc_Key_flag = 0;
	}
	
	if (++PCF8591_ADC_cnt >= 900)
	{
		PCF8591_ADC_cnt = 0;
		PCF8591_ADC_TimeFlag = 1;
		
	}
	
	
	if (++Frequency_cnt >= 500)	//1S
	{
		Frequency_cnt = 0;
		Frequency_ReadData_Flag = 1;
	}
	
}

void LED_Disp(unsigned char uc_LED)
{
	//LED
    P2 &= 0X1F;   //关573
	P0 =  ~uc_LED;   //预送数据
	P2 |= 0X80;   //开相应573
	P0 =  ~uc_LED;    //送数据
	P2 &= 0X1F;   //关573
}